<?php
/* Home controller 
** thaodx@gmail.com
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends CI_Controller {
	public $layout_view = 'layout/ajax';
	// get facebook comment, like from URL
	public function fb()
	{
		$url = base64_decode($_POST['url']);
		if(strstr($url, ',')){
			$url = explode(',', $url);
		}
		$this->load->helper('social');
		//array('http://www.haivl.com/photo/58799', 'http://www.haivl.com/photo/603154', 'http://www.haivl.com/photo/604056')
		$fb = getFacebookCount($url);
        echo json_encode($fb);
	}
	public function topfbcmt()
	{
		$url = $_POST['url'];
		$this->load->helper('social');
		
		$fb = getTopFacebookComment($url, false);
        echo json_encode($fb);
        die;
	}
	public function loadmore($cateCode = '', $page = 0, $lastId = 0)
	{

		
		$this->load->model('Post_model', 'homeModel');
		$this->load->helper(array('datetime', 'permalink', 'social', 'image'));

		$this->load->model('Post_model', 'postModel');
		$this->load->model('Cate_model', 'cateModel');
		$cateInfo = $this->cateModel->getAllCate($page,7);			
		if(empty($cateInfo)) header('location:'.BASE_URL);
		$cate_act = $this->cateModel->getCateBySlug($cateCode);
		$cate_act = $cate_act[0];
		$additionTitle = '';
		if($page > 0){
		 	$additionTitle = ' - Trang '. ($page / CATE_LIMIT + 1);
		}
			$latestPost = $this->postModel->getPostByCate($cate_act['category_id'], $page, CATE_LIMIT, $lastId);
	       	$data = array('latestPost' => $latestPost);
		
		if(!empty($data)){
			$html = $this->layout->view('ajax_loadmore', $data, true);
		}
		else{
			$html = '';
		}
		// $lastItem = end($latestPost);
		// $result = array(
		// 	'html' => $html,
		// 	'lastid' => intval($lastItem->id),
		// 	'nextpage' => base64_encode($lastItem->id.'-'.$offset)
		// 	);

		die($html);
	}
}